package model

import (
	"errors"
	"github.com/dailing/levlog"
)

type Song struct {
	Id             int64  `json:"id"`
	Name           string `xorm:"Index" json:"name"`
	OriginalSinger int64  `xorm:"Index" json:"original_singer"`
	Cover          int64  `json:"cover_id"`
	Lyric          int64  `json:"lyric_id"`
	Midi           int64  `json:"midi_id"`
	Accompany      string `json:"accompany"`
	Owner          int64  `json:"-"`
}

func (s *Song) SetOwner(userId int64) {
	s.Owner = userId
}

func (s *Song) GetOwner() int64 {
	if s.Owner <= 0 {
		ts := Song{Id: s.Id}
		if _, err := engine.Get(&ts); err != nil {
			levlog.E(err)
			return 0
		}
		s.SetOwner(ts.Owner)
	}
	return s.Owner
}

func (*Song) Delete() error {
	panic("implement me")
}

func (s *Song) Get() error {
	ok, err := engine.Get(s)
	if !ok {
		return errors.New("no such record")
	}
	return err
}

func (s *Song) Update() error {
	_, err := engine.Update(s)
	return err
}

func (s *Song) Insert() error {
	_, err := engine.Insert(s)
	return err
}

func (*Song) TableName() string {
	return "song"
}
